package com.yc.happ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.happ.entity.Elder;
import com.yc.happ.entity.OrderItem;
import com.yc.happ.entity.OrderList;
import com.yc.happ.entity.Volunteer;
import com.yc.happ.service.BackHandleService;
import com.yc.happ.service.BackService;
import com.yc.happ.serviceImpl.BackHandleServiceImpl;
import com.yc.happ.serviceImpl.BackServiceImpl;

@WebServlet("/happinesshome/back/backhandle/*")
public class BackServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2223814721254224201L;
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if("delete".equals(getRequestFlag(request))){
			doOrderListDelete(request,response);
		}else if("refresh".equals(getRequestFlag(request))){
			doRefresh(request,response);
		}else if("choose".equals(getRequestFlag(request))){
			doChoose(request,response);
		}else if("assess".equals(getRequestFlag(request))){
			doAssess(request,response);
		}
	}
	private void doAssess(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String score=request.getParameter("score");
		String aid=request.getParameter("aid");
		String vid=request.getParameter("vid");
		String eid=request.getParameter("eid");
		BackHandleService backHandle=new BackHandleServiceImpl();
		boolean b=backHandle.assessOrderItem(score, aid, vid, eid);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		
	}
	private void doChoose(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String oid=request.getParameter("oid");
		String vid=request.getParameter("vid");
		String eid=request.getParameter("eid");
		BackHandleService backHandle=new BackHandleServiceImpl();
		boolean b=backHandle.chooseOrderList(oid, vid, eid);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		
	}
	private void doRefresh(HttpServletRequest request, HttpServletResponse response) throws IOException {
		BackService back=new BackServiceImpl();
		
		List<OrderList> listOrderList=back.getAllOrderList(1);
		List<Elder> listelder=back.getAllElder(1);
		List<Volunteer> listVolunteer=back.getAllVolunteer(1);
		List<OrderItem> listOrderItem=back.getAllOrderItem(1);
		request.getSession().setAttribute("backorderlist", listOrderList);
		request.getSession().setAttribute("backelder", listelder);
		request.getSession().setAttribute("backvolunteer", listVolunteer);
		request.getSession().setAttribute("backorderitem", listOrderItem);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		jsonStr="{\"success\":true}";
		out.print(jsonStr);
		out.flush();
		out.close();
		
	}
	private void doOrderListDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String oid=request.getParameter("oid");
		String vid=request.getParameter("vid");
		String eid=request.getParameter("eid");
		BackHandleService backHandle=new BackHandleServiceImpl();
		boolean b=backHandle.deleteOrderList(oid, vid, eid);
		String jsonStr=null;
		PrintWriter out=response.getWriter();
		if(b==true){
			jsonStr="{\"success\":true}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}else{
			jsonStr="{\"success\":false}";
			out.print(jsonStr);
			out.flush();
			out.close();
		}
		
	}
}
